package com.cwnu.service.impl;

import com.cwnu.mapper.UserMapper;
import com.cwnu.entity.User;
import com.cwnu.service.UserService;
import com.cwnu.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User findByUserName(String username) {
        User user = userMapper.findByUserName(username);
        return user;
    }

    @Override
    public void register(String username, String password, Integer age, String school) {
        // 可以在此处对密码进行加密处理，为了简便，我们不作加密处理
        userMapper.add(username, password, age, school);
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    @Override
    public void updatePassWord(String newPassWord) {
        // 根据ThreadLocalUtil获取当前登录的用户的信息（id）
        Map<String, Object> mp = ThreadLocalUtil.get();
        Integer loginUserId = (Integer) mp.get("id");
        // 根据id更新密码
        userMapper.updatePassWord(newPassWord, loginUserId);
    }

    @Override
    public void logOut() {
        Map<String, Object> mp = ThreadLocalUtil.get();
        Integer userId = (Integer) mp.get("id");
        userMapper.deleteUser(userId);
    }
}
